<?php

namespace App\Console\Commands;

use App\model\Area;
use App\model\Coustomer;
use App\model\Industry;
use App\Repository\CustomerRepository;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class JinriCommand extends Command
{

     protected  $key=[
         'xinxing'=> [
             'token'=>'6c351de4f2c2e1b344c67063f8bb4e2cd5670082',
             'key'=>'OFpST1JZQTVITkM1',
             'project_id'=>17
         ],
         'xinxing1'=> [
             'token'=>'48f585bb1df03147423b2413d5bab75bade7fa0f',
             'key'=>'RkFHMUo0RklJU0RU',
             'project_id'=>17
         ],
         'shiyou'=> [
             'token'=>'8acde9998b7ace3e80ec140311d3ee66116f7f13',
             'key'=>'UVFYNk9KRzUwOE9O',
             'project_id'=>4
         ],
         'shiyou2'=> [
             'token'=>'f8581be0d5867ed7cc89abd641f0b086aa630b63',
             'key'=>'MUszVzM0TUdKM0VC',
             'project_id'=>15
         ],
         'shiyou3'=> [
             'token'=>'1b4a4c1e3e6fc1e7b855227f3d63edcc810433a1',
             'key'=>'QVVRSEg0RVBHUDdF',
             'project_id'=>15
         ],
         'zhaoshang'=> [
             'token'=>'a74afd963ac0d3d6ca846d4aba6db406debe8050',
             'key'=>'UzZRWkhKWUxJRDM2',
             'project_id'=>13
         ],
         'huateng'=> [
             'token'=>'598d2eaa749f0264bb0099362687610564844ff0',
             'key'=>'QUFRWTNXOEdQNTA0',
             'project_id'=>19
         ],
         'huateng1'=> [
             'token'=>'de4cf6122de218aeaa2a7526396b8e0ae9997482',
             'key'=>'MjVBNlkyMEw0UDlN',
             'project_id'=>19
         ],
         'huateng2'=> [
             'token'=>'04622c2bb5921c604df6f0a2fc8a0187c72b3758',
             'key'=>'UVcyV1JKTE41NFBM',
             'project_id'=>23
         ],
         'huateng3'=> [
             'token'=>'87e482c5279de9522f3a5b90607ec2ad65126ead',
             'key'=>'R0RXN0s2VDUxRlNB',
             'project_id'=>23
         ],
         'huatengRui'=> [
             'token'=>'bce2aaa3302cac04fe6b8a269ec8a1933be3b8a0',
             'key'=>'TVhZOEQ0Mjg5Rk9V',
             'project_id'=>20
         ],

         'xinxing2'=>[
             'token'=>'5685a7ca0f4dcac954338bd0f1cb1827bfb4ca07',
             'key'=>'MjZJU05MUDlHUlkw',
             'project_id'=>22
         ],
         'xinxing3'=>[
             'token'=>'dce85204b3ea6897f64700eba8fd3e0abd33851c',
             'key'=>'WEpQRTAyRENZN1VE',
             'project_id'=>22
         ],
    ];
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'jinri:project {project}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
    protected  $client;
    protected   $customerRepository;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Client $client,CustomerRepository $customerRepository)
    {
        $this->client=$client;
        parent::__construct();
        $this->customerRepository=$customerRepository;
    }


//头条获取数据接口

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       if(!isset($this->key[$this->argument('project')])){
           $this->error('类型不对');
           return ;
       } ;
       $config=$this->key[$this->argument('project')];
       $host="https://feiyu.oceanengine.com/";
       $api="/crm/v2/openapi/pull-clues/";
        $date_s=Carbon::today()->toDateString();
        $date_e=Carbon::tomorrow()->toDateString();
        $now= time();
        $start_date = $date_s;
        $end_date= $date_e;
        $source_data = $api.'?start_time='.$start_date.'&end_time='.$end_date.' '.$now;
        $key=$config['key'];
        $tureOrFalse=true;
        $page=1;
     while ($tureOrFalse){
        $result= $this->client->get('https://feiyu.oceanengine.com/crm/v2/openapi/pull-clues/',[
            'headers'=>[
                'signature'=>base64_encode(hash_hmac('sha256', $source_data, $key)),
                'timestamp'=>$now,
                'Access-Token'=>$config['token'],
            ],
            'query'=>[
                'start_time'=>$start_date,
                'end_time'=>$end_date,
                'page'=>$page,
                'page_size'=>100,
            ]
        ]);
        $page++;
        $app=json_decode( $result->getBody()->getContents(),true);

        if (!empty($app['data'])){

            foreach ($app['data'] as $v){
                /*$coustomer=Coustomer::whereMobile($v['telphone'])
                    ->where('project_id',$config['project_id'])
                    ->where('created_at','>',Carbon::today())
                    ->first();*/

                $coustomer=Coustomer::where('clue_id','=',$v['clue_id'])
                    ->first();

                if (!$coustomer){
                    $coustomer=new Coustomer();
                    $coustomer->name=$v['name'];
                    $coustomer->mobile=$v['telphone'];
                    if ($v['location']){
                        $city=explode('+',$v['location']);
                        $coustomer->city1=$city[0];
                        $coustomer->city2=$city[1];
                    }
                    $coustomer->source_id=9;
                    $coustomer->project_id=$config['project_id'];
                    if (isset($v['remark_dict']['所在行业'])){
                        $industry=Industry::firstOrCreate(['name'=>$v['remark_dict']['所在行业'],'project_id'=>$config['project_id']]);
                        $coustomer->industry_id=$industry->industry_id;

                    }

                    $coustomer->clue_id=$v['clue_id'];
                    if (isset($v['remark_dict']['需求面积'])){
                        $area=Area::firstOrCreate(['name'=>$v['remark_dict']['需求面积']]);
                        $coustomer->area_id=$area->id;
                    }
                    if (isset($v['remark_dict']['需求的面积'])){
                        $area=Area::firstOrCreate(['name'=>$v['remark_dict']['需求的面积']]);
                        $coustomer->area_id=$area->id;
                    }
                    if (isset($v['remark_dict']['所需面积'])){
                        $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需面积']]);
                        $coustomer->area_id=$area->id;
                    }
                    if (isset($v['remark_dict']['所需求的面积'])){
                        $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需求的面积']]);
                        $coustomer->area_id=$area->id;
                    }
                    if (isset($v['remark_dict']['所需厂房面积'])){
                        $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需厂房面积']]);
                        $coustomer->area_id=$area->id;
                    }
                    $coustomer->adv_name=$v['ad_plan_name'];
                    $coustomer->external_url=$v['external_url'];
                    $coustomer->sex=$v['gender'];
                    $coustomer->save();
                    $this->info($coustomer->customer_id);
                    //销售数据
                    $this->customerRepository->sendXiaoshou($coustomer);
                }

            }
        }else{
            $tureOrFalse=false;
        }
     }
    }

}
